package com.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.demo.beans.Buiding;
import com.demo.beans.Device;
import com.demo.beans.Page;
import com.demo.config.RedisUtil;
import com.demo.service.BuiDingService;
import com.demo.service.DeviceService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.List;


@Controller
@Api
public class HomeController {
    @Autowired
    private BuiDingService buiDingService;;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    RedisUtil redisUtil;
    @GetMapping("/index")
    public String index(@RequestParam(value = "page",defaultValue = "1") int pageIndex,
                        @RequestParam(value = "buidingId",required = false) Long buidingId,
                        Model model){
        int pageSize = 3;
        Page<Device> deviceList = deviceService.getDeviceList(pageIndex, pageSize, buidingId);
        model.addAttribute("page",deviceList);
        //把集合转为json字符串
        String json = JSON.toJSONString(deviceList);
        //存入redis
        redisUtil.set("json",json);

        List<Buiding> bList;
        //从redis缓存中读取数据
        String json1 = (String) redisUtil.get("bList");
        if(json1==null){
            //从数据库中读取数据
            bList = buiDingService.getBuidingList();
            redisUtil.set("bList",JSON.toJSONString(bList));
        }else {
            //从redis缓存中读取数据
            bList = JSON.parseArray(json1, Buiding.class);
        }
        model.addAttribute("bList",bList);
        return "index";
    }

    @GetMapping("/add")
    public String add(Model model) {
       //从redis中读取缓存
        String json = (String) redisUtil.get("bList");
        //如果redis中没有数据
        List<Buiding> bList;
        if(json==null){
            //从数据库中读取数据
            bList = buiDingService.getBuidingList();
            redisUtil.set("bList",bList);
        }else {
            //从redis缓存中读取数据
            bList = JSON.parseArray(json, Buiding.class);
        }
        model.addAttribute("bList",bList);
        return "add";
    }

    @PostMapping("/doadd")
    public String doAdd(@RequestParam("principal") String principal,
                        @RequestParam("supplier") String supplier,
                        @RequestParam("brand") String brand,
                        @RequestParam("buidingId") Long buidingId,
                        @RequestParam("addTime") String addTime) {
        Device device = new Device();
        device.setPrincipal(principal);
        device.setSupplier(supplier);
        device.setBrand(brand);
        device.setHospitalId(buidingId);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            device.setAdd_time(sdf.parse(addTime));
        } catch (Exception e) {
            e.printStackTrace();
        }
        deviceService.addDevice(device);
        //重定向到首页
        return "redirect:/index";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(@RequestParam("id") Long id) {
        int i = deviceService.updateDevice(id);
        return JSONObject.toJSONString(i);
    }
    @RequestMapping("/update2")
    @ResponseBody
    public String update2(@RequestParam("id") Long id) {
        int i = deviceService.updateDevice2(id);
        return JSONObject.toJSONString(i);
    }
}
